package com.twang.stock.design.impl;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.design.IndexDataTask;
import com.twang.stock.dto.IndexedInfo;
import com.twang.stock.entity.CompanyStockMinutes;
import com.twang.stock.entity.ex.HotTagStockInfo;
import com.twang.stock.mapper.CompanyStockMinutesMapper;
import com.twang.stock.mapper.HotTagMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.service.TodayQinXuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 自选涨跌板块
 */

@Service
@Slf4j
public class ZiXuanBanKuaiTask extends IndexedInfo implements IndexDataTask {
    public static String cache_key = "ZiXuanBanKuaiTask";

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TodayQinXuService todayQinXuService;

    /**
     * AI-核心
     */
    public static String TYPE_ZX_TAG_AI_HX="ZX_TAG_AI_HX";

    /**
     * AI-龙头/情绪
     */
    public static String TYPE_ZX_TAG_AI_QXLT="ZX_TAG_AI_QXLT";


    /**
     * 中特估-核心
     */
    public static String TYPE_ZX_TAG_ZTG_HX="ZX_TAG_ZTG_HX";

    /**
     * 中特估-龙头/情绪
     */
    public static String TYPE_ZX_TAG_ZTG_QXLT="ZX_TAG_ZTG_QXLT";

    /**
     * 高度-妖股
     */
    public static String TYPE_ZX_TAG_YAO_GU="ZX_TAG_YAO_GU";

    @Resource
    private HotTagMapper hotTagMapper;

    @Resource
    private CompanyStockMinutesMapper companyStockMinutesMapper;

    public static String cache_key_ai_hx="cache_key_ai_hx";
    public static String cache_key_ai_hx_flag="cache_key_ai_hx_flag";

    public static String cache_key_ai_qxlt="cache_key_ai_qxlt";

    public static String cache_key_ai_qxlt_flag="cache_key_ai_qxlt_flag";

    public static String cache_key_ztg_hx="cache_key_ztg_hx";
    public static String cache_key_ztg_hx_flag="cache_key_ztg_hx_flag";

    public static String cache_key_ztg_qxlt="cache_key_ztg_qxlt";
    public static String cache_key_ztg_ztg_qxlt_flag="cache_key_ztg_qxlt_flag";

    public static String cache_key_yao_gu="cache_key_yao_gu";
    public static String cache_key_yao_gu_flag="cache_key_yao_gu_flag";






    @Override
    public void execute(ModelAndView modelAndView, CountDownLatch latch) throws Exception {
        try {

//            Object cacheKeyAiHx = redisUtil.get(cache_key_ai_hx);
//            List<HotTagStockInfo> ai_hx= JSONObject.parseArray(cacheKeyAiHx.toString(), HotTagStockInfo.class);
//            Boolean cacheKeyAiHxFlag = (Boolean) redisUtil.get(cache_key_ai_hx_flag);
//            modelAndView.addObject("ai_hx", ai_hx);
//            modelAndView.addObject("ai_hx_flag", cacheKeyAiHxFlag);
//
//
//
//            Object cacheKeyAiQxlt = redisUtil.get(cache_key_ai_qxlt);
//            List<HotTagStockInfo> ai_qxlt= JSONObject.parseArray(cacheKeyAiQxlt.toString(), HotTagStockInfo.class);
//            Boolean cacheKeyAiQxltFlag = (Boolean) redisUtil.get(cache_key_ai_qxlt_flag);
//            modelAndView.addObject("ai_qxlt", ai_qxlt);
//            modelAndView.addObject("ai_qxlt_flag", cacheKeyAiQxltFlag);
//
//
//            Object cacheKeyZtgHx = redisUtil.get(cache_key_ztg_hx);
//            List<HotTagStockInfo> ztg_hx= JSONObject.parseArray(cacheKeyZtgHx.toString(), HotTagStockInfo.class);
//            Boolean ztg_hx_flag = (Boolean) redisUtil.get(cache_key_ztg_hx_flag);
//            modelAndView.addObject("ztg_hx", ztg_hx);
//            modelAndView.addObject("ztg_hx_flag", ztg_hx_flag);
//
//
//
//            Object cacheKeyZtgQxlt = redisUtil.get(cache_key_ztg_qxlt);
//            List<HotTagStockInfo> ztg_qxlt= JSONObject.parseArray(cacheKeyZtgQxlt.toString(), HotTagStockInfo.class);
//            Boolean ztg_qxlt_flag = (Boolean) redisUtil.get(cache_key_ztg_ztg_qxlt_flag);
//            modelAndView.addObject("ztg_qxlt", ztg_qxlt);
//            modelAndView.addObject("ztg_qxlt_flag", ztg_qxlt_flag);
//
//
//
//            Object cacheKeyYaogu = redisUtil.get(cache_key_yao_gu);
//            List<HotTagStockInfo> yao_gu= JSONObject.parseArray(cacheKeyYaogu.toString(), HotTagStockInfo.class);
//            Boolean yao_gu_flag = (Boolean) redisUtil.get(cache_key_yao_gu_flag);
//            modelAndView.addObject("yao_gu", yao_gu);
//            modelAndView.addObject("yao_gu_flag", yao_gu_flag);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            log.info("大盘涨跌情况 执行完成，当前剩余条数:{}", latch.getCount());
            latch.countDown();
        }
    }

    public Boolean  setZhangDie(List<HotTagStockInfo> list){
        AtomicReference<Boolean> isAllSz= new AtomicReference<>(Boolean.TRUE);
        list.stream().forEach(item->{
            CompanyStockMinutes companyStockMinutes =companyStockMinutesMapper.queryByCode(item.getCode());
            if(null!=companyStockMinutes){
                item.setZhangdie(companyStockMinutes.getZhangDie());
            }else{
                item.setZhangdie(BigDecimal.ZERO);
            }
            if(item.getZhangdie().compareTo(BigDecimal.ZERO)<0){
                isAllSz.set(Boolean.FALSE);
            }
        });

        return isAllSz.get();
    }
}
